package com.lw.demo;

import java.time.Duration;
import java.time.Instant;
import java.util.stream.LongStream;

/**
 * @author LW
 * @2021/9/18 16:33
 */
public class TestParallel {
    public static void main(String[] args) {
        //采用并行流（JDK8以后的推荐做法）
        //并行流底层还是Fork/Join框架，只是任务拆分优化得很好。

        Instant start = Instant.now();
        long result = LongStream.rangeClosed(0, 100000000L).parallel().reduce(0, Long::sum);
        Instant end = Instant.now();
        System.out.println("耗时：" + Duration.between(start, end).toMillis() + "ms");//88ms

        System.out.println("结果为：" + result);

    }
}
